JavaScript Reflect Methods

您所在的位置:网站首页 javascript reflect JavaScript Reflect Methods

JavaScript Reflect Methods

#JavaScript Reflect Methods| 来源: 网络整理| 查看: 265

Reflect is a built-in JavaScript object which gives access to other methods for interceptable operations. JavaScript Reflect methods are the same as those of proxy handlers and are static just like the Math object. As it is not a JavaScript function object, so it’s not constructible. It cannot be used with the JavaScript new operator and cannot be invoked as a function. 

Static methods: JavaScript Reflect object provides the following static methods.

Reflect.apply(target, thisArgument, argumentsList)Reflect.construct(target, argumentsList[, newTarget])Reflect.defineProperty(target, propertyKey, attributes)Reflect.deleteProperty(target, propertyKey)Reflect.get(target, propertyKey[, receiver])Reflect.getOwnPropertyDescriptor(target, propertyKey)Reflect.getPrototypeOf(target)Reflect.has(target, propertyKey)Reflect.isExtensible(target)Reflect.ownKeys(target)Reflect.preventExtensions(target)Reflect.set(target, propertyKey, value[, receiver])Reflect.setPrototypeOf(target, prototype)

Below are examples of the JavaScript Reflect object methods.

Example 1: The following example demonstrates Reflect.has(target, propertyKey) method which returns a Boolean if the target parameter has the property.

HTML

                 const car = {      name: 'Maruti',      color: 'white',      model: '2019'    }      // If the target has the property, it returns a Boolean.        // It acts like the in operator in a function.    console.log("car object has 'color' property :",                              Reflect.has(car, 'color'));    console.log("car object has 'haircut' property :",                             Reflect.has(car, 'haircut'));    

Output :

car object has 'color' property : true car object has 'haircut' property : false

Example 2: The following example demonstrates Reflect.ownKeys(target) method which returns an array of the target parameter’s own property keys.

HTML

                  const car = {      name: 'Maruti',      color: 'white',      model: '2019'    }      // It returns an array of the target's    // own property keys.    console.log("All the keys of the object 'car':",                                    Reflect.ownKeys(car));  

Output :

All the keys of the object 'car': (3) ["name", "color", "model"]

Example 3 : The following example demonstrates the Reflect.set(target, propertyKey, value[, receiver]) method that assigns values to properties returning a true Boolean value. If Reflect.set is successful, it returns true else it returns false.

HTML

                   const car = {      name: 'Maruti',      color: 'white',      model: '2019'    }      // It assigns values to properties.     // It returns true if the set or update is successful.    console.log("Insert key : 'airbags' to the object 'car':",                              Reflect.ownKeys(car, 'airbags', 3));    console.log(car);    

Output : 

Insert key : 'airbags' to the object 'car': (3) ["name", "color", "model"] {name: "Maruti", color: "white", model: "2019"}

Example 4: The following example demonstrates the Reflect.apply(target, thisArgument, argumentsList) which calls a target function with parameters in the argumentsList. 

HTML

                           // It calls a target function with the     // argumentsList as parameter.    console.log(Reflect.apply(String.fromCharCode,                    undefined, [103, 101, 101, 107, 115]));         console.log(Reflect.apply(Math.floor, undefined, [3.14]));         console.log(Reflect.apply(RegExp.prototype.exec, /ab/,                                     ['confabulation']).index);        console.log(Reflect.apply(''.charAt, 'geeksforgeeks', [6]));         

Output : 

geeks 3 4 oMy Personal Notes arrow_drop_up


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3